$(function () {
    $("#form-update").Validform({
        tiptype: 1,
        showAllError: true,
        datatype: {
            "empty": /^\s*$/
        },
        callback: function (form) {
        	if($('#roleId').next().find('input').val()==''){
            	layer.msg('请选择角色')
            	return false;
            }
            layer.confirm('您确定要提交表单吗?',function(){
                _submit();
            })
            return false;
        }

    });
});

$(document).ready(function () {
        var employee = employeeStr;
        $('#loginname').val(employee.loginName);
        $('#empName').val(employee.empName);
        $('#code').val(employee.code);
        $('#marriageStatus').val(employee.marriageStatus);
        $('#sex').val(employee.sex);
        $('#address').val(employee.homeAddress);
        $('#phone').val(employee.mobilePhone);
        $('#email').val(employee.email);
        $('#officePhone').val(employee.officePhone);
        $('#professionalRanks').val(employee.professionalRanks);
        $('#idCardType').val(employee.idCardType);
        $('#idCardNo').val(employee.idCardNo);
        $('#orgId').val(employee.orgId);
        $("#orgId").change(function () {
        	var orgId = $.trim($("#orgId").val());
            if(orgId==undefined) orgId="";
            $("#roleId").empty();
            $.ajax({
                type: "get",
                url: baseUrl + '/api/role/findRoleList?orgId=' + orgId,
                dataType: "json",
                success: function (result) {
                    if (!result.flag) {
                        layer.msg(" 获取失败！原因：" + result.message);
                        return;
                    }
                    $.each(result.results, function (i, val) {
                        $("#roleId").append('<option value="' + val.id + '">' + val.name + '</option>');
                    });
                    $("#roleId").next().find('input').val($("#roleId").find("option[value='"+employee.roleId+"']").text());
                }
            });
        });
        $("#orgId").change();
    });

_submit = function () {
    var data = {};
    var user = {};
    var employee = {};
    user.loginName = $('#loginname').val();
    data.user = user;

    employee.id = parseInt($('#employeeId').val());
    employee.empName = $('#empName').val();
    employee.code = $('#code').val();
    employee.marriageStatus = $('#marriageStatus').val();
    employee.sex = $('#sex').val();
    employee.homeAddress = $('#address').val();
    employee.mobilePhone = $('#phone').val();
    employee.email = $('#email').val();
    employee.officePhone = $('#officePhone').val();
    employee.professionalRanks = $('#professionalRanks').val();
    employee.idCardType = $('#idCardType').val();
    employee.idCardNo = $('#idCardNo').val();
    data.employee = employee;

    data.orgId = $.trim($("#orgId").val());
    data.roleId = $('#roleId').val();
    data.groupId = $('#groupId').val();

    $.ajax({
        type: "post",
        url: baseUrl + '/api/employee/update',
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (result) {
            if (!result.flag) {
                layer.msg("更新失败！原因：" + result.message);
                return;
            }
            parent.location.reload();
            window.colse();
        }
    });
}